package com.beyondsoft.dao;

import java.util.ArrayList;
import java.util.List;

import com.beyondsoft.model.Book;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class DBManager {

	public static final Uri CONTENT_URI = Uri.parse("content://com.beyondsoft/books");
	public static String[] argments = new String[1];
	public static String SELECTION;
	public static String[] COLUMNS ;
	static {
		StringBuilder buffer = new StringBuilder();
		buffer.append("id");
		buffer.append("=?");
		SELECTION = buffer.toString();
		COLUMNS = new String[] { "id", "name", "author" };
	}

	public static Book findBookById(ContentResolver contentResolver, Integer id) {

		Book book = new Book();
		argments[0] = id + "";
		
		Cursor cursor = contentResolver.query(CONTENT_URI, null, SELECTION,
				argments, null);
		Log.i("sys", SELECTION + argments);
		if (cursor.moveToFirst()) {

			String _name = cursor.getString(cursor
					.getColumnIndexOrThrow(COLUMNS[1]));
			String _author = cursor.getString(cursor
					.getColumnIndexOrThrow(COLUMNS[2]));
			book.setId(id);
			book.setBook_name(_name);
			book.setAuthor(_author);
			cursor.close();
			
			return book;
		}
		cursor.close();
		
		return null;
	}

	public static List<Book> findAll(ContentResolver contentResolver) {

		List<Book> books = new ArrayList<Book>();
		Book book;
		Cursor cursor = null;
		
		try {
			cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
			if (cursor.getCount() < 1) {
				return null;
			}
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				book = new Book();
				String _id = cursor.getString(cursor
						.getColumnIndexOrThrow(COLUMNS[0]));
				String _name = cursor.getString(cursor
						.getColumnIndexOrThrow(COLUMNS[1]));
				String _author = cursor.getString(cursor
						.getColumnIndexOrThrow(COLUMNS[2]));
				book.setId(Integer.parseInt(_id));
				book.setBook_name(_name);
				book.setAuthor(_author);
				books.add(book);
				cursor.moveToNext();
			}
		} finally {
			if (cursor != null)
				cursor.close();
		}
		
		return books;
	}

	public static Uri insert(ContentResolver contentResolver, Book book) {
		
		ContentValues value = new ContentValues();
		
		value.put(COLUMNS[1], book.getBook_name());
		value.put(COLUMNS[2], book.getAuthor());
		Uri uri = contentResolver.insert(CONTENT_URI, value);
		
		return uri;
	}

	public static boolean update(ContentResolver contentResolver, Book book) {
		
		Log.i("sys", book.getAuthor() + book.getBook_name() + book.getId());
		ContentValues value = new ContentValues();
		
		value.put(COLUMNS[1], book.getBook_name());
		value.put(COLUMNS[2], book.getAuthor());
		int id = contentResolver.update(CONTENT_URI, value, SELECTION,
				new String[] { book.getId() + "" });
		
		return id > 0;
	}

	public static boolean delete(ContentResolver contentResolver, String id) {
		
		int re = contentResolver.delete(CONTENT_URI, SELECTION,
				new String[] { id });
		
		return re > 0;

	}

}
